﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Lookup
{
    public partial class selectcustomer : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        private CtrtCustomerBll customerBll = new CtrtCustomerBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                //BindList(string.Empty,1);
            }
        }
        protected void query(object sender, EventArgs e)
        {
            int isAgency = 0;
            if (cbisAgency.Checked) { isAgency = 1; }
            AspNetPager1.CurrentPageIndex = 1;
            BindList(customerNameOrCode.Text.Trim(), isAgency,1);         
        }

        private void BindList(string queryParm,int isAgency,int PageIndex)
        {
            int RecordCount = 0;
            string WhereClause = "";

            List<CtrtCustomerEntity> userEntitys = new List<CtrtCustomerEntity>();

            if (Request.QueryString["rank"] == null)
            {
                if (queryParm == "")
                {
                    WhereClause += " and CustomerID in (select CustomerID from Cms_Ctrt_CustomerUser where UserID='" + base.CurrentUserID + "' )";
                }
                else
                {
                    WhereClause += "  and CustomerID in (select CustomerID from Cms_Ctrt_CustomerUser where UserID='" + base.CurrentUserID + "' ) and (CustomerName like '%" + queryParm + "%' or customerCode like '%" + queryParm + "%' or CustomerAddress like '%" + queryParm + "%')";
                }
            }

            if (Request.QueryString["rank"] == "all")
            {
                if (queryParm != "")
                {
                    WhereClause += " and (CustomerName like '%" + queryParm + "%' or customerCode like '%" + queryParm + "%' or CustomerAddress like '%" + queryParm + "%')";
                }
            }

            if (isAgency == 0) { WhereClause += " and CustomerName not like  '%公司%'"; }

            userEntitys = customerBll.GetAll<CtrtCustomerEntity>(WhereClause, " CustomerName asc", AspNetPager1.PageSize, PageIndex, out RecordCount);
            this.RepeaterUsers.DataSource = userEntitys;
            this.RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            int isAgency = 0;
            if (cbisAgency.Checked) { isAgency = 1; }
            BindList(customerNameOrCode.Text.Trim(),isAgency,AspNetPager1.CurrentPageIndex);
        }

    }
}
